package com.example.aqarapp.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import aqarapp.R;

public class EditTextClearTools {
    private static int xUp = 0;
    private static boolean account = false;
    public static void addClearListener(final EditText et, final Button et2){
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //如果有输入内容长度大于0那么显示clear按钮
                String str = s + "" ;
                if (s.length() > 0){
                    et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.a, 0, R.drawable.del_btn,0);
                    if(et.getText().toString().trim().length() > 0 ) {
                        String phone_number = et.getText().toString().trim();
                        String number = "[1][34578]\\d{9}";
                        String ems = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
                        if(phone_number.matches(number) && et.getText().toString().trim().length() == 11) {
//                            et2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.password, 0, R.drawable.yanzhengma,0);
                            et2.setEnabled(true);
                            account = true;
                        }
                        else if(phone_number.matches(ems)) {
//                            et2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.password, 0, R.drawable.yanzhengma,0);
                            et2.setEnabled(true);
                            account = true;
                        }
                        else {
//                            et2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.password, 0, R.drawable.yanzhengma_null,0);
                            et2.setEnabled(false);
                            account = false;
                        }

                    }
                    else {
//                        et2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.password, 0, R.drawable.yanzhengma_null,0);
                        et2.setEnabled(false);
                        account = false;
                    }
                }else{
//                    et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.a, null, null, null);
                    et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.a, 0, 0,0);
                    account = false;
                }
            }
        });

        et.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // 获取点击时手指抬起的X坐标
                    xUp = (int) event.getX();
                    /*Rect rect = new Rect();
                    getGlobalVisibleRect(rect);
                    rect.left = rect.right - 50;*/
                    // 当点击的坐标到当前输入框右侧的距离小于等于 getCompoundPaddingRight() 的距离时，则认为是点击了删除图标
                    if ((et.getWidth() - xUp) <= et.getCompoundPaddingRight()) {
                        if (!TextUtils.isEmpty(et.getText().toString())) {
                            et.setText("");
                        }
                    }
                }else if(event.getAction() == MotionEvent.ACTION_DOWN && et.getText().length()!=0){
                    et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.a, 0, R.drawable.del_btn,0);
                }else if(et.getText().length()!=0){
                    et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.a, 0, R.drawable.del_btn,0);
                }
                return false;
            }
        });
    }

}
